//
//  HomeViewController.m
//  QuickCure
//
//  Created by 马文星 on 16/4/28.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "HomeViewController.h"
#import "MLMenuButton.h"
#import "CycleScrollView.h"
#import "CaseDetailViewController.h"
#import "UIAlertView+Additions.h"
#import "SCLAlertView.h"

@interface HomeViewController ()

/** 天气View */
@property (weak, nonatomic) IBOutlet UIImageView *weatherImageview;
@property (weak, nonatomic) IBOutlet UILabel *weatherTitle;
@property (weak, nonatomic) IBOutlet UILabel *weatherCity;
@property (weak, nonatomic) IBOutlet UILabel *weatherT;
@property (weak, nonatomic) IBOutlet UILabel *weatherDate;

/** Scrollview */
@property (weak, nonatomic) IBOutlet UIView *topVIew;


/** 6个按钮 */


@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //轮播期
    [self loadCircleScrollViewImage];
    
    //加载天气
    [self loadWeather];
    
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav"]];
}

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    
    
    

}

//请求天气
- (void)loadWeather{

    WEAKSELF
    [[NetWorkManager sharedManager] loadWeatherWithCompleteBlock:^(NSString *nightPictureUrl, NSString *weather, NSString *city, NSString *temperature, NSString *dateString) {
        
        loggerWarn(@"tupian  = %@",nightPictureUrl)
        
        [weakSelf.weatherImageview sd_setImageWithURL:[NSURL URLWithString:nightPictureUrl] placeholderImage:[UIImage imageNamed:@"qing"]];
        weakSelf.weatherTitle.text = weather;
        weakSelf.weatherT.text = temperature;
        weakSelf.weatherDate.text = dateString;
        
        //加下划线
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:city];
        
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [content length])];
        
        weakSelf.weatherCity.attributedText = content;
  
    }];
}

//请求滚动scrollview图片
- (void)loadCircleScrollViewImage{
    
    NSArray *imagesArray = @[@"http://7xsiiq.com2.z0.glb.clouddn.com/h1.png",
                             @"http://7xsiiq.com2.z0.glb.clouddn.com/h2.png",
                             @"http://7xsiiq.com2.z0.glb.clouddn.com/h3.png"];
    
    NSMutableArray *imageViewArray = [NSMutableArray array];

    for (int i = 0; i < imagesArray.count; ++i) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.topVIew.frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imagesArray[i]] placeholderImage:[UIImage imageNamed:@"good_image_default"] options:SDWebImageRetryFailed];
        
        [imageViewArray addObject:imageView];
    }
    
    CycleScrollView *cycleScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.topVIew.frame.size.height) animationDuration:4];
    
    cycleScorllView.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
    
    cycleScorllView.scrollView.showsHorizontalScrollIndicator = NO;
    
    [self.topVIew addSubview:cycleScorllView];
    
    cycleScorllView.totalPagesCount = ^NSInteger(void){
        return imageViewArray.count;
    };
    cycleScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        
        return imageViewArray[pageIndex];
    };
    
    //因为原型图没有给出点击做什么事，但是呢，我们还是要留下接口的，因为一般的轮播器点击都会有交互的
    cycleScorllView.TapActionBlock = ^(NSInteger pageIndex){

        
        loggerWarn(@"点击了第%ld张",pageIndex);
        
    };

}


#pragma mark  - 懒加载
- (IBAction)clickMenuButton:(MLMenuButton *)menuButton {
    
    BOOL isLogon =  [MLDefaults boolForKey:kIsUserLoginKey];
    
    NSString *caseDetailString = nil;
    
    if (!isLogon) {
        
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        
        [alert showError:self title:@"用户未登录！"
                subTitle:@"抱歉！您必须登录账户后才能查询信息！"
        closeButtonTitle:@"确定" duration:0.0f];
        
        
    }else{
        
        //肿瘤
        if (menuButton.tag == 1001) {
            
            NSLog(@"肿瘤");
            caseDetailString = @"肿瘤";
        }
        //心血管
        else if (menuButton.tag == 1002){
            
            NSLog(@"心血管");
            caseDetailString = @"心血管";
        }
        //神经科
        else if (menuButton.tag == 1003){
            
            NSLog(@"神经科");
            caseDetailString = @"神经科";
        }
        //血液科
        else if (menuButton.tag == 1004){
            
            NSLog(@"血液科");
            caseDetailString = @"血液科";
        }
        //骨科
        else if (menuButton.tag == 1005){
            
            NSLog(@"骨科");
            caseDetailString = @"骨科";
        }
        
        //公益
        else if (menuButton.tag == 1006){
            
            NSLog(@"公益");
            caseDetailString = @"公益";
        }
        
        CaseDetailViewController *caseVC  = (CaseDetailViewController *)[UIViewController creatVCByStoryBoardWIthSBName:@"Main" VCIdentify:@"CaseDetailViewController"];

        caseVC.caseDetailString = caseDetailString;
        [self.navigationController pushViewController:caseVC animated:YES];
        
    }
}


@end
